package com.ohos.carbon.slice.guidelines;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbPalette;
import ohos.agp.components.*;
import ohos.agp.components.element.ElementScatter;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;

import java.util.Map;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;

public class ButtonsUsageActivity extends AbilitySlice {

    public Image back_button1;
    public Text text_title1;
    public Button button;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_activity_buttonsusage);
        initView();
        checkbox();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0) {
            if (MainAbilitySlice.all.containsKey("Button/Usage")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("Button/Usage")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("Button/Usage", integer);
                        pf.flushSync();
                    }
                }
            } else {
                pf.putInt("Button/Usage", 1);
                pf.flushSync();
            }
        } else {
            pf.putInt("Button/Usage", 1);
            pf.flushSync();
        }
    }

    private void checkbox() {
        ShapeElement elementButtonOn = new ShapeElement();
        elementButtonOn.setRgbColor(RgbPalette.RED);
        elementButtonOn.setShape(ShapeElement.RECTANGLE);
        elementButtonOn.setCornerRadius(0.0f);

        ShapeElement elementButtonOff = new ShapeElement();
        elementButtonOff.setRgbColor(RgbPalette.BLACK);
        elementButtonOff.setShape(ShapeElement.RECTANGLE);
        elementButtonOff.setCornerRadius(0.0f);

        StateElement checkElement = new StateElement();
        checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_CHECKED}, elementButtonOn);
        checkElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, elementButtonOff);

        Checkbox checkbox = (Checkbox) findComponentById(ResourceTable.Id_settings_checkbox);
        checkbox.setButtonElement(checkElement);
    }

    private void initView() {
        back_button1 = (Image) findComponentById(ResourceTable.Id_back_button1);
        text_title1 = (Text) findComponentById(ResourceTable.Id_title_text1);
        text_title1.setText("Settings");
        back_button1.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow_white));
        button = (Button) findComponentById(ResourceTable.Id_back_button3);
        button.setVisibility(Component.HIDE);
        back_button1.setClickedListener(component -> onBackPressed());
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}